#include "stdafx.h"
#include "SoundEffect3D.h"
#include "AudioManager.h"
#include "../EdgeMath/Math.h"
#include "../EdgeMath/Vectors.h"
#include <fmod_codec.h>
#include <fmod_common.h>
// set the time in the file the sound should loop (in milliseconds)
// pass 0 to stop the sound from looping
/*virtual*/ void SoundEffect3D::setIsLooped(bool value) /*override*/
{
	FMOD_MODE mode;
	sound->getMode(&mode);
	if (value)
		mode |= FMOD_LOOP_NORMAL;	
	else
		mode &= ~FMOD_LOOP_NORMAL;
	sound->setMode(mode);
}

/*virtual*/ SoundEffect3D::~SoundEffect3D(void)
{
	AudioManager* pAudio = AudioManager::GetInstance();
	for (unsigned int i = 0; i < pAudio->activeElements.size(); i++)
	{
		if (pAudio->activeElements[i] == this)
		{
			pAudio->activeElements.erase(pAudio->activeElements.begin() + i);
			break;
		}
	}
	sound->release();
}

SoundEffect3D::SoundEffect3D(void)
{
	AudioManager::GetInstance()->activeElements.push_back(this);
}
// updates the sound
/*virtual*/ void SoundEffect3D::Update(void) /*override*/
{
	if (channel)
	{
		channel->set3DAttributes(reinterpret_cast<const FMOD_VECTOR*>(&position), reinterpret_cast<const FMOD_VECTOR*>(&velocity));
		channel->setMode(FMOD_3D);
		sound->setMode(FMOD_3D);
		channel->set3DMinMaxDistance(minDistance, maxDistance);
	}
}